package cn.signalpha.modules.base.controller.api;

import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.modules.base.entity.Bulletin;
import cn.signalpha.modules.base.service.BulletinService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_BASE_NAME + "/wap/bulletin")
@Api(value = "通知公告", tags = "通知公告")
public class ApiBulletinController extends BaseController {

	private final BulletinService bulletinService;

	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "")
	public R<IPage<Bulletin>> page(Query query) {
		LambdaQueryWrapper<Bulletin> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.select(Bulletin::getId, Bulletin::getTitle, Bulletin::getBrief, Bulletin::getImg, Bulletin::getCreateTime);
		return R.data(bulletinService.page(Condition.getPage(query), queryWrapper.orderByDesc(Bulletin::getId)));
	}

	@GetMapping("/list")
	@ApiOperation(value = "列表", notes = "获取最新的5条记录")
	public R<List<Bulletin>> list() {
		return R.data(bulletinService.listCache());
	}

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "")
	public R<Bulletin> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(bulletinService.getByCache(id));
	}

}
